var throbber;
$(function(){
	throbber = $('#throbber');
	init();
});

function init() {
	// init tabs
	$('.tab-box').tabs({
		
	});
}

function cookieGet(name) {
	return $.cookie(name);
}
function cookieSet(name, value) {
	$.cookie(name, value);
}
function getAccordionIndex(id) {
	var el   = $('#'+id);
	var indx = parseInt(cookieGet(id + '-accordion-index'));
	if (! el.children('h2').eq(indx).length) {
		indx = 0;
	}
	return indx;
}
function setAccordionIndex(id, indx) {
	cookieSet(id + '-accordion-index', indx);
}

function urlStripHost(url) {
	if (url.match(/^[fh]t+ps?:\/+/)) {
		url = url.replace(/^[fh]t+ps?:\/+[^\/]+/, '');
	}
	return url;
}

function defaultBefore() {
	return function(){
		showThrobber();
	};
}

function defaultComplete() {
	return function(req) {
		content = $('#content');
		hideThrobber();
		
		if (req.status == 200) {
			content.html(req.responseText);
			init();
		} else {
			var div = $('<div class="error" />');
			div.append('<h2>' + req.status + '. ' + req.statusText + '</h2>');
			content.html(div);
		}
	};
}

function GET(url, complete, before) {
	if (!before)   before   = defaultBefore();
	if (!complete) complete = defaultComplete();

	var now = new Date();
	url += "?cb=" + now.getTime();
	$.ajax({
		url: 		url,
		type: 		'get',
		beforeSend: function(){before()},
		complete: 	function(req){complete(req)}
	});
}
function PUT(url, data, complete, before) {
	if (! before)   before   = defaultBefore();
	if (! complete) complete = defaultComplete();
	
	$.ajax({
		url: url,
		type:       'post',
		data:       data,
		beforeSend: function(){before()},
		complete:   function(req){complete(req)}
	});
}

function showThrobber() {
	$('#throbber').animate({
		top:0
	}, 200)
}
function hideThrobber() {
	$('#throbber').animate({
		top:-50
	}, 100)
}
function formRemoteDialog(url, buttons, options) {
	options = $.extend(options, {
		url: url
	});
	formDialog(false, buttons, options);
}
function formDialog(content, buttons, options) {
	options = $.extend(options, {
		buttons: buttons,
		dialogClass: 'form-dialog'
	});
	dialog(content, options);
}
function formDialogSubmit(dialog) {
	var form = dialog.children('form').eq(0);
	
	var btnSet  = dialog.parent().find('.ui-dialog-buttonset');
	var buttons = btnSet.find('button');
	buttons
		.addClass('loading')
		.attr('disabled', 'disabled');
	form.submit();
}
function dialog(content, options) {
	defaultOptions = {
		modal: true,
		width: 600,
		height: 400,
		resizable: false,
		beforeClose: function(e, ui) {
			$(this).dialog('destroy');
		}
	}
	options = options ? options : {};
	var div = $('<div>').html(content);
	
	// add resultFrame
	var resultFrame = $('<iframe>');
	resultFrame
		.appendTo(div)
		.attr('id',   'formDialogResultFrame')
		.attr('name', 'formDialogResultFrame')
		.hide();
	
	
	options = $.extend(defaultOptions, options);
	div.dialog(options);
	if (options.url) {
		$.ajax({
			url: options.url,
			beforeSend: function(){				
				div.addClass('loading');
			},
			complete: function(req) {
				div.removeClass('loading');
				var html = req.status + '.' + req.statusText;
				if (req.status == 200) {
					html = req.responseText;
				}
				div.html(html);
				div.append(resultFrame);
			}
		});
	}
}

function remoteDelete(url, id, ondelete) {
	if (! confirm('Da li želite da obrišete objekat?')) {
		return;
	}
	GET(url, function(req){
		hideThrobber();
		if (req.status == 200) {
			ondelete(id);
		} else {
			httpError(req);
		}
	});
	
}

function httpError(req) {
	alert(req.status + '. ' + req.statusText);
}

function doIFramePostBack(response) {
	var btnSet  = $('.ui-dialog-buttonset');
	var buttons = btnSet.find('button');
	var msg     = btnSet.find('.message');
	if (! msg.length) {
		msg = $('<div class="message">').prependTo(btnSet);
	}
	
	buttons.removeClass('loading').removeAttr('disabled');
	
	msg.text(response.message);
	if (response.status == 200) {
		msg.attr('class', 'message ok');
		location.reload();
	} else {
		msg.attr('class', 'message error');
	}
	
}
function flash(message, type) {
	type = type ? type : 'error';
	
	$('#flash')
		.text(message).attr('class', type)
		.animate({
			top:0
		}, {
			duration: 200,
			complete: function(){
				setTimeout(function(){
					$('#flash').animate({
						top:-50
					}, 100)
				}, 4000);
			}
		});
}